#include <bits/stdc++.h>
#define ll long long
#define i128 __int128
using namespace std;
ll a0[200005],d[200005],x[10];
ll T,n,m,K,a[200005],c[200005];
ll dp[200005],lim;
bool g[200005];
void dfs(ll p,ll l,ll r,ll rd,ll wt)
{
	dp[p] = g[p] = 0;
	if(l == r)
	{
		dp[p] = l,g[p] = (l <= lim ? 0 : 1);
		return;
	}
	ll mid = l + r >> 1;
	dfs(p << 1,l,mid,rd - 1,wt);
	dfs(p << 1 | 1,mid + 1,r,rd - 1,wt);
	if(g[p << 1 | d[p]])
	{
		if(dp[p << 1] == wt || dp[p << 1 | 1] == wt)
			dp[p] = wt;
		else
			dp[p] = dp[p << 1 | d[p]],g[p] = 1;
	}
	else
	{
		//cout << a[dp[p << 1 | d[p]]]<<rd;
		if((dp[p << 1 | d[p]] == wt && wt > lim) || a[dp[p << 1 | d[p]]] >= rd)
			dp[p] = dp[p << 1 | d[p]];
		else
			dp[p] = dp[(p << 1 | d[p]) ^ 1],g[p] = g[(p << 1 | d[p]) ^ 1];
	}
	//cout << p << " "<<l << r << " "<<dp[p]<<" "<<rd<<a[5] <<endl;
}
void solve()
{
	for(ll i = 0;i < 4;i++)
		scanf("%lld",&x[i]);
	for(ll i = 1;i <= n;i++)
		a[i] = a0[i] ^ x[i % 4];
	//for(ll i = 1;i <= n;i++)
	//	cout << a[i]<<" ";
	//cout<<endl;
	ll ret = 0;
	for(ll i = 1;i <= m;i++)
	{
		ll w = 0;
		lim = c[i];
		while((1LL << w) < c[i])
			w++;
		//cout << "xxxxxxxxx"<<w<<endl;
		ll sum = 0;
		for(ll i = 1;i <= (1 << w);i++)
		{
			//cout << "   "<<i<<a[i] << "\n",
			dfs(1,1,(1 << w),w,i);
			if(dp[1] == i)
				/*cout << "r"<<i<<endl,*/sum += i;
		}
	//printf("\n");
	//	printf("- %lld  %lld\n\n",i,sum);
		ret = ret ^ (sum * i);
	}
	//printf("\n\n");
	printf("%lld\n",ret);
}
int main()
{
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	scanf("%lld%lld",&n,&m);
	for(ll i = 1;i <= n;i++)
		scanf("%lld",&a0[i]);
	for(ll i = 1;i <= m;i++)
		scanf("%lld",&c[i]);
	while((1LL << K) < n)
		K++;
	for(ll i = 1;i <= K;i++)
		for(ll j = (1LL << (K - i));j < (1LL << (K - i + 1));j++)
			scanf("%1lld",&d[j]);
	scanf("%lld",&T);
	while(T--)
		solve();
	return 0;
}